/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import communication.AppInfo;
import communication.AppListElement;
import domen.Application;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Stateless;
import javax.imageio.ImageIO;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Danilo
 */
@Stateless
public class ApplicationFacade extends AbstractFacade<Application> {
    @PersistenceContext(unitName = "SrbDroidPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public ApplicationFacade() {
        super(Application.class);
    }
    
    public List<String> getDisabledApp(){
        Query q = em.createQuery("SELECT a.name FROM Application a WHERE a.enabled = :enabled");
        q.setParameter("enabled", false);
        return q.getResultList();
    }
    
    public List<String> getApplicationsNames(){
        Query q = em.createQuery("SELECT a.name FROM Application a ORDER BY a.name DESC");
        return q.getResultList();
    }
    
    public int getApplicationIdForName(String name){
        Query q = em.createQuery("SELECT a.applicationId FROM Application a WHERE a.name = :name");
        q.setParameter("name", name);
        return  (Integer) q.getSingleResult();
    }

    public Application findApplicationByName(String name) {
        Query q = em.createNamedQuery("Application.findByName");
        q.setParameter("name", name);
        return (Application) q.getSingleResult();
    }

    public List<String> searchForApplication(String appName) {
        Query q = em.createQuery("SELECT a.name FROM Application a WHERE a.name LIKE :name");
        q.setParameter("name", "%"+appName);
        return q.getResultList();
    }

    public List<AppListElement> getTopListApplication() {
        List<AppListElement> list = new LinkedList<AppListElement>();
        Query q = em.createQuery("SELECT a.applicationId, a.name, a.image1 FROM Application a ORDER BY a.downloads DESC");
        q.setMaxResults(5);
        List<Object[]> result = q.getResultList();
        for (Object[] objects : result) {
            int id = (Integer)objects[0];
            String name = (String) objects[1];
            byte[] image1 = (byte[]) objects[2];
            list.add(new AppListElement(id, name, image1));
        }
        return list;
    }    
    
    public AppInfo getApplicationInfo(int id) {
        Query q = em.createQuery("SELECT a.applicationId, a.name, a.description, a.image1, a.image2, a.minAndReq, a.downloads, a.rating FROM Application a WHERE a.applicationId=:id");
        q.setParameter("id", id);
        Object[] result = (Object[]) q.getSingleResult();
        AppInfo appInfo = new AppInfo((Integer) result[0], (String) result[1], (String) result[2], (byte[])result[3], (byte[])result[4], (String)result[5], (Integer)result[6], (Double)result[7]);
        return appInfo;
    }
}
